﻿using System;
using System.Linq;
using System.Runtime.Caching;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TfsGiggle.Core.IntegrationTest
{
    public class MockVersionControlServer : IVersionControlServer
    {
        private static Lazy<MockVersionControlServer> _instance = new Lazy<MockVersionControlServer>(() => new MockVersionControlServer());

        private MockVersionControlServer() { }

        public static MockVersionControlServer Instance
        {
            get { return _instance.Value; }
        }

        public bool ServerItemExists(string path, ItemType itemType)
        {
            return true;
        }

        public IItem GetItem(string path)
        {
            var changeset = MemoryCache.Default["changeset"] as IChangeset;
            if (changeset == null || changeset.Changes == null || changeset.Changes.Length == 0) return null;
            return (from p in changeset.Changes where p.Item.ServerItem.Contains(path) select p.Item).SingleOrDefault();
        }

        public IChangeset GetChangeset(int changesetId, bool includeChanges, bool includeDownloadInfo)
        {
            return MemoryCache.Default["changeset"] as IChangeset;
        }

        public IChangeset[] QueryHistory(string path, VersionSpec versionSpec, int deletionId, RecursionType recursion, string user, VersionSpec versionFrom, VersionSpec versionTo, int maxCount, bool includeChanges, bool slotMode)
        {
            var changeset = MemoryCache.Default["changeset"] as IChangeset;
            if (changeset == null || changeset.Changes == null || changeset.Changes.Length == 0)
            {
                return new MockChangeset[] { new MockChangeset() };
            }
            else
            {
                return new MockChangeset[] { (MockChangeset)changeset };
            }
        }
    }
}
